package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class TRUNC extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    if (paramArrayOfObject.length == 1)
    {
      localObject1 = paramArrayOfObject[0];
      if (!(localObject1 instanceof Number))
        return null;
      return new Integer((int)((Number)localObject1).doubleValue());
    }
    Object localObject1 = paramArrayOfObject[0];
    Object localObject2 = paramArrayOfObject[1];
    double d = 1.0D;
    if ((localObject1 instanceof Number) && (localObject2 instanceof Number))
    {
      if (((Number)localObject2).intValue() < 0)
      {
        for (i = 1; i <= -1 * ((Number)localObject2).intValue(); ++i)
          d *= 10.0D;
        if (((Number)localObject1).doubleValue() >= 0.0D)
          return new Double(()(((Number)localObject1).doubleValue() / d) * d);
        return new Double(()(((Number)localObject1).doubleValue() / d) * d);
      }
      if (((Number)localObject2).intValue() == 0)
      {
        if (((Number)localObject1).doubleValue() >= 0.0D)
          return new Long(()((Number)localObject1).doubleValue());
        return new Long(()((Number)localObject1).doubleValue());
      }
      for (int i = 1; i <= ((Number)localObject2).intValue(); ++i)
        d *= 10.0D;
      if (((Number)localObject1).doubleValue() >= 0.0D)
        return new Double(()(((Number)localObject1).doubleValue() * d) / d);
      return new Double(()(((Number)localObject1).doubleValue() * d) / d);
    }
    return Primitive.ERROR_NAME;
  }

  public Function.Type getType()
  {
    return Function.MATH;
  }

  public String getCN()
  {
    return "TRUNC(number,num_digits):将数字的小数部分截去，返回整数。\nnumber:需要截尾取整的数字。\nnum_digits:用于指定取整精度的数字。\n示例:\nTRUNC(8.9) 等于 8\nTRUNC(-8.9) 等于 -8\nTRUNC(PI()) 等于 3\n";
  }

  public String getEN()
  {
    return "TRUNC(number,num_digits): Truncates a number to an integer by removing the fractional part of the number.\nNumber is the number you want to truncate.\nNum_digits is a number specifying the precision of the truncation. The default value for num_digits is 0 (zero).\n\nExample:\n   TRUNC(8.9) = 8\n   TRUNC(-8.9) = -8\n   TRUNC(PI()) = 3\n";
  }
}